<?php

use Illuminate\Database\Capsule\Manager as DB;

class User_BankController extends CommonController
{
    // 获取提现账户信息
    public function infoAction()
    {
        $type = $this->get('type');
        $bankInfo = memberbankModel::info($type);
        $bankInfo['id_number'] = $bankInfo['identity_card_number'];
        json(['code' => 1, 'msg' => '提现账户', 'data' => $bankInfo]);
    }


    public function editAction()
    {
        $params = $this->getPost();
        $rules = [
            'type' => ['role'=>'required', 'msg'=>'提现账户类型'],
            'real_name' => ['role'=>'required', 'msg'=>'真实姓名'],
            'card_no' => ['role'=>'required', 'msg'=>'账号'],
        ];
        if ($params['type'] === 'bank') {
            $rules['id_number'] = ['func' => 'isIdcard', 'role'=>'required', 'msg'=>'身份证号码'];
            $rules['bank_name'] = ['role'=>'required', 'msg'=>'开户行'];
            $rules['branch_name'] = ['role'=>'required', 'msg'=>'支行名称'];
        }
        if ($params['type'] === 'alipay') {
            $params['bank_name'] = '支付宝账户';
        }
        try {
            Validate::check($params, $rules);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '编辑成功', 'data' => memberbankModel::edit($params)]);
    }

    public function supportBankAction()
    {
        $banks = DB::table('sys_config')
            ->where('type', 9)
            ->where('key', 'banks')
            ->value('value') ?: '';
        json(['code' => 1, 'msg' => '支持银行', 'data' => explode(',', $banks)]);
    }
}
